package com.flca.frw.service;

import org.perf4j.StopWatch;
import org.perf4j.slf4j.Slf4JStopWatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flca.frw.context.AppContext;
import com.flca.frw.context.RequestContext;
import com.flca.frw.validators.ValidateException;
import com.google.inject.Inject;

public abstract class BaseExternalService {

	private static Logger logger = LoggerFactory.getLogger(BaseExternalService.class);

	@Inject private AppContext appContext;
	
	public BaseExternalService() {
	}

	/** 
	 * The application can override this method to find out if the user has the correct role for this service
	 * @param ctx
	 * @return
	 */
	protected boolean checkInRole(RequestContext ctx) {
		return true;
	}

	/**
	 * The application can override this method to validate a checksum for example an 
	 * @param ctx
	 * @return
	 */
	protected boolean isValid(RequestContext ctx) {
		return true;
	}


	protected Exception handleValidationError(ValidateException ex, RequestContext ctx) {
		logger.warn(ex.getMessage());
		return new Exception(ex.getMessage());
	}

	protected StopWatch logStart(String msg, RequestContext context) {
		logger.info("starting " + msg);
		return new Slf4JStopWatch();
	}

	protected void logFinish(StopWatch stopwatch, String msg, RequestContext context) {
		stopwatch.stop(msg);
	}

}
